import 'package:flutter/material.dart';
import 'package:flutter_base/base/controller/base_controller.dart';
import 'package:flutter_base/config/app_routes.dart';
import 'package:flutter_base/http/http_export.dart';
import 'package:flutter_base/user/app_user_manager.dart';
import 'package:flutter_base/utils/loading/loading_utils.dart';
import 'package:get/get.dart';

class WalletLogic extends BaseController {
  // 余额
  final balance = 1232.obs;
  
  // 选择的充值金额
  final selectedAmount = 0.obs;
  
  // 选择的支付方式
  final selectedPaymentMethod = "mastercard".obs;
  
  // 充值档位列表
  final rechargeOptions = [
    RechargeOption(coins: 20, price: 18, isFirstCharge: true),
    RechargeOption(coins: 50, price: 50, isLimitedTime: true),
    RechargeOption(coins: 100, price: 100),
    RechargeOption(coins: 180, price: 180),
    RechargeOption(coins: 360, price: 360),
    RechargeOption(coins: 600, price: 600),
  ];
  
  // 支付方式列表
  final paymentMethods = [
    PaymentMethod(id: "mastercard", name: "Mastercard", icon: "mastercard"),
    PaymentMethod(id: "paypal", name: "Paypal", icon: "paypal"),
    PaymentMethod(id: "wise", name: "Wise", icon: "wise"),
  ];

  @override
  void onInit() {
    super.onInit();
    fetchWalletData();
    
    // 默认选择第一个充值选项
    if (rechargeOptions.isNotEmpty) {
      selectedAmount.value = rechargeOptions[0].coins;
    }
  }

  // 获取钱包数据
  void fetchWalletData() async {
    doLoading();
    
    // 获取用户余额
    if (AppUserManager().user.value?.balance != null) {
      balance.value = AppUserManager().user.value!.balance!;
    }
    
    await Future.delayed(const Duration(milliseconds: 500));
    
    doFinished();
  }

  // 选择充值金额
  void selectAmount(int amount) {
    selectedAmount.value = amount;
  }
  
  // 选择支付方式
  void selectPaymentMethod(String methodId) {
    selectedPaymentMethod.value = methodId;
  }

  // 前往充值详情
  void goCoinDetails() {
    // Get.toNamed(AppRoutes.coinDetails);
    // LoadingUtils.showToast('Coin Details功能开发中');

    Get.toNamed(AppRoutes.walletCoinDetails);
  }

  // 执行充值
  void doRecharge() {
    if (selectedAmount.value <= 0) {
      LoadingUtils.showToast('请选择充值金额');
      return;
    }
    
    LoadingUtils.showLoading(status: '充值中...');
    
    // 模拟充值过程
    Future.delayed(const Duration(seconds: 2), () {
      LoadingUtils.dismiss();
      LoadingUtils.showToast('充值成功');
      
      // 更新余额
      balance.value += selectedAmount.value;
      
      // 重置选择
      if (rechargeOptions.isNotEmpty) {
        selectedAmount.value = rechargeOptions[0].coins;
      }
    });
  }
}

// 充值选项模型
class RechargeOption {
  final int coins;
  final int price;
  final bool isFirstCharge;
  final bool isLimitedTime;
  
  RechargeOption({
    required this.coins, 
    required this.price, 
    this.isFirstCharge = false, 
    this.isLimitedTime = false
  });
}

// 支付方式模型
class PaymentMethod {
  final String id;
  final String name;
  final String icon;
  
  PaymentMethod({
    required this.id, 
    required this.name, 
    required this.icon
  });
}

// 交易记录模型
enum TransactionType { deposit, withdrawal, purchase, other }

class TransactionModel {
  final int id;
  final double amount;
  final TransactionType type;
  final String title;
  final DateTime date;
  
  TransactionModel({
    required this.id,
    required this.amount,
    required this.type,
    required this.title,
    required this.date,
  });
} 